$(function () {
    $("#page").load("/html/page.html");
    selectGroup();
    searchEducation();
    searchUniversity()
});

function searchEducation() {
    let result = myAjax('/education/selectAll', "post", {})
    let html = " <option value='0'>全部</option>";
    for (let i = 0; i < result.length; i++) {
        html += '<option value="' + result[i].id + '">' + result[i].category + '</option>';
    }
    $("#educationList").html(html);
}
function searchUniversity() {
    let result = myAjax('/university/selectAll', "post", {})
    let html = " <option value='0'>全部</option>";
    for (let i = 0; i < result.list.length; i++) {
        html += '<option value="' + result.list[i].id + '">' + result.list[i].academy + '</option>';
    }
    $("#graduate").html(html);
}
function selectGroup() {
    let result = myAjax('/group/selectAll', 'post', {});
    let html = " <option value='0'>全部</option>";
    for (let i = 0; i < result.list.length; i++) {
        html += ' <option value="' + result.list[i].id + '">' + result.list[i].name + '</option>';
    }
    $("#group").html(html);
}

function searchAll() {
    let data = {
        username: $("#username").val(),
        sex: $("#sex").find("option:selected").text(),
        age: $("#age").val(),
        education: $("#educationList").find("option:selected").text(),
        graduate: $("#graduate").find("option:selected").text(),
        startRegistrationTime: $("#startRegistrationTime").val(),
        endRegistrationTime: $("#endRegistrationTime").val(),
        group: $("#group").find("option:selected").text(),
        pageSize: $.trim($("#pageSize").val()),
        pageNum: $.trim($("#pageNum").val()),
    }
    let result = myAjax('/user/findAll', "get", data)
    show(result.list)
    $("#pageCount").html(result.number);
}

function show(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += '   <tr> ' +
            '  <td><input type="checkbox" name="isCheck" value="' + data[i].id + '">' + data[i].id + '</td>\n' +
            '        <td>' + data[i].username + '</td>\n' +
            '        <td>' + data[i].name + '</td>\n' +
            '        <td>' + data[i].sex + '</td>\n' +
            '        <td>' + data[i].age + '</td>\n' +
            '        <td>' + data[i].address + '</td>\n' +
            '        <td>' + data[i].phone + '</td>\n' +
            '        <td>' + data[i].group + '</td>\n' +
            '        <td>' + data[i].education + '</td>\n' +
            '        <td>' + data[i].graduate + '</td>\n' +
            '        <td>' + data[i].state + '</td>\n' +
            '        <td>' + data[i].registrationTime + '</td>\n' +
            '        <td><input type="button" style="width: 60px;border-radius: 5px" value="删除" onclick="del(' + data[i].id + ')"></td>' +
            '    </tr>';
    }
    $("#findAll").html(html);
}

function del(id) {
    let result = myAjax("/user/delete", "get", {id: id})
    if (result === 1) {
        alert("删除成功");
        searchAll();
    } else {
        alert("操作失败")
    }
}
